function varargout = ImageLab(varargin)
%IMAGELAB M-file for ImageLab.fig
%      IMAGELAB, by itself, creates a new IMAGELAB or raises the existing
%      singleton*.
%
%      H = IMAGELAB returns the handle to a new IMAGELAB or the handle to
%      the existing singleton*.
%
%      IMAGELAB('Property','Value',...) creates a new IMAGELAB using the
%      given property value pairs. Unrecognized properties are passed via
%      varargin to ImageLab_OpeningFcn.  This calling syntax produces a
%      warning when there is an existing singleton*.
%
%      IMAGELAB('CALLBACK') and IMAGELAB('CALLBACK',hObject,...) call the
%      local function named CALLBACK in IMAGELAB.M with the given input
%      arguments.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to mn_help ImageLab

% Last Modified by GUIDE v2.5 31-Mar-2013 15:42:25

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
    'gui_Singleton',  gui_Singleton, ...
    'gui_OpeningFcn', @ImageLab_OpeningFcn, ...
    'gui_OutputFcn',  @ImageLab_OutputFcn, ...
    'gui_LayoutFcn',  [], ...
    'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before ImageLab is made visible.
function ImageLab_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   unrecognized PropertyName/PropertyValue pairs from the
%            command line (see VARARGIN)

% Choose default command line output for ImageLab
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes ImageLab wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = ImageLab_OutputFcn(hObject, eventdata, handles)
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --------------------------------------------------------------------
function file_Callback(hObject, eventdata, handles)
% hObject    handle to file (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --------------------------------------------------------------------
function mn_view_Callback(hObject, eventdata, handles)
% hObject    handle to mn_view (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --------------------------------------------------------------------
function mn_image_Callback(hObject, eventdata, handles)
% hObject    handle to mn_image (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --------------------------------------------------------------------
function mn_help_Callback(hObject, eventdata, handles)
% hObject    handle to mn_help (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --------------------------------------------------------------------
function mn_help_about_Callback(hObject, eventdata, handles)
% hObject    handle to mn_help_about (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
helpdlg(sprintf(['ImageLab 1.0 - Copyright TuanND - CPIQ Lab' '\n' 'tuan dot d dot nguyen et okstate dot edu']), 'Credit');


% --------------------------------------------------------------------
function mn_image_adjust_Callback(hObject, eventdata, handles)
% hObject    handle to mn_image_adjust (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --------------------------------------------------------------------
function mn_analysis_rgb_Callback(hObject, eventdata, handles)
% hObject    handle to mn_analysis_rgb (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if isRGBImg(handles.img)
    figure('Name', 'RGB Channels', 'NumberTitle','off');
    subplot(2,2,1);imshow(handles.img);title('Original Image');
    subplot(2,2,2);imshow(handles.img(:,:,1));title('R Channel');
    subplot(2,2,3);imshow(handles.img(:,:,2));title('G Channel');
    subplot(2,2,4);imshow(handles.img(:,:,3));title('B Channel');
else
    disp('Not RGB image');
    warndlg('Not RGB image');
end

% --------------------------------------------------------------------
function mn_file_open_Callback(hObject, eventdata, handles)
% hObject    handle to mn_file_open (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[filename pathname] = uigetfile({'*.jpg;*.tif;*.png;*.gif;*.bmp','All Image Files';...
    '*.*','All Files' },'Image Selector', '../imgs/');
if isequal(filename,0)
    disp('User selected Cancel')
else
    disp(['User selected ', fullfile(pathname, filename)])
    handles.img = imread([pathname filename]);
    handles.pathname = pathname;
    handles.filename = filename;
    updateScreen(handles);
%     handles.pathname = pathname;
%     handles.filename = filename;
%     imshow(handles.img, 'parent', handles.axesImg);
%     [rows cols channels] = size(handles.img);
%     set(handles.mainfigure, 'Name', ['ImageLab-' '[' filename ']-[' num2str(rows) 'x' num2str(cols) 'x' num2str(channels) ']']);
%     if isRGBImg(handles.img)
%         set(handles.mn_image_mode_rgb, 'Checked', 'on');
%         set(handles.mn_image_mode_grayscale, 'Checked', 'off');
%     else
%         set(handles.mn_image_mode_rgb, 'Checked', 'off');
%         set(handles.mn_image_mode_grayscale, 'Checked', 'on');
%     end
end
guidata(hObject, handles);


% --------------------------------------------------------------------
function mn_file_save_Callback(hObject, eventdata, handles)
% hObject    handle to mn_file_save (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if ~isequal(handles.img, 0)
    imwrite(handles.img, [handles.pathname handles.filename]);
else
    disp('Nothing to save');
end


% --------------------------------------------------------------------
function mn_file_saveas_Callback(hObject, eventdata, handles)
% hObject    handle to mn_file_saveas (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if ~isequal(handles.img, 0)
    [new_filename new_pathname] = uiputfile({'*.jpg;*.tif;*.png;*.gif;*.bmp','All Image Files';...
        '*.*','All Files' },'Save Image',...
        [handles.pathname 'new_' handles.filename]);
    if ~isequal(new_filename, 0)
        handles.filename = new_filename;
        handles.pathname = new_pathname;
        imwrite(handles.img, [handles.pathname handles.filename]);
    else
        disp('User cancel');
    end
else
    disp('Nothing to save');
end


% --------------------------------------------------------------------
function ms_file_quit_Callback(hObject, eventdata, handles)
% hObject    handle to ms_file_quit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
delete(handles.mainfigure);

% --------------------------------------------------------------------
function mn_image_adjust_brctr_Callback(hObject, eventdata, handles)
% hObject    handle to mn_image_adjust_brctr (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[brightness contrast] = BrightnessContrast();
%g(x,y) = f(x,y) * alpha + beta
[rows cols channels] = size(handles.img);
handles.undoimg = handles.img;
alpha = 1 + contrast/100;
beta = brightness;
for i = 1:rows
    for j = 1:cols
        handles.img(i,j,:) = handles.img(i,j,:) * alpha + beta;
    end
end
imshow(handles.img, 'parent', handles.axesImg);
guidata(hObject, handles);

% --------------------------------------------------------------------
function uiopenfile_ClickedCallback(hObject, eventdata, handles)
% hObject    handle to uiopenfile (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
mn_file_open_Callback(hObject, eventdata, handles);

% --------------------------------------------------------------------
function uisave_ClickedCallback(hObject, eventdata, handles)
% hObject    handle to uisave (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
mn_file_save_Callback(hObject, eventdata, handles);


% --------------------------------------------------------------------
function mn_image_adjust_hsv_Callback(hObject, eventdata, handles)
% hObject    handle to mn_image_adjust_hsv (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if isRGBImg(handles.img)
    handles.undoimg = handles.img;
    [hue saturation intensity] = AdjustHSIDlg();
    img_rgb = double(handles.img)/255;
    img_hsv = rgb2hsv(img_rgb);
    if ~isequal(hue, 0)
        img_hsv(:,:,1) = img_hsv(:,:,1) * (1 + hue/100);
    end
    if ~isequal(saturation, 0)
        img_hsv(:,:,2) = img_hsv(:,:,2) * (1 + saturation/100);
    end
    if ~isequal(intensity, 0)
        img_hsv(:,:,3) = img_hsv(:,:,3) * (1 + intensity/100);
    end
    handles.img = hsv2rgb(img_hsv);
    imshow(handles.img, 'parent', handles.axesImg);
else
    disp('Not RGB');
    warndlg('Not RGB image');
end
guidata(hObject, handles);

% --------------------------------------------------------------------
function mn_image_adjust_cielab_Callback(hObject, eventdata, handles)
% hObject    handle to mn_image_adjust_cielab (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if isRGBImg(handles.img)
    handles.undoimg = handles.img;
    [hue chroma lightness] = CIELABAdjust();
    img_lab = rgb2lab(handles.img);
    [rows cols channels] = size(img_lab);
    
    L = img_lab(:,:,1);
    a = img_lab(:,:,2);
    b = img_lab(:,:,3);
    
    Cab = (a.^2 + b.^2).^ (1/2);
    hab (a ~= 0) = atan(b(a ~= 0)./a(a ~= 0));
    hab (a == 0) = sign(b(a==0)) * pi/2;
    hab = reshape(hab, rows, cols);
    
    hab = hab * (1 + hue/100);
    Cab = Cab * (1 + chroma/100);
    
    L = L * (1 + lightness/100);
    a = cos(hab) .* Cab;
    b = sin(hab) .* Cab;
    img_lab(:,:,1) = L;
    img_lab(:,:,2) = a;
    img_lab(:,:,3) = b;
    handles.img = lab2rgb(img_lab);
    imshow(uint8(handles.img), 'parent', handles.axesImg);
else
    disp('Not RGB, do nothing');
    warndlg('Not RGB image');
end
guidata(hObject, handles);


% --------------------------------------------------------------------
function uiundo_ClickedCallback(hObject, eventdata, handles)
% hObject    handle to uiundo (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
mn_edit_undo_Callback(hObject, eventdata, handles);

% --------------------------------------------------------------------
function mn_edit_Callback(hObject, eventdata, handles)
% hObject    handle to mn_edit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --------------------------------------------------------------------
function mn_edit_undo_Callback(hObject, eventdata, handles)
% hObject    handle to mn_edit_undo (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if ~isequal(handles.undoimg,0)
    handles.img = handles.undoimg;
    handles.undoimg = 0;
%     imshow(handles.img, 'parent', handles.axesImg);
    updateScreen(handles);    
else
    disp('Do nothing');
end
guidata(hObject, handles);


% --------------------------------------------------------------------
function mn_image_mode_Callback(hObject, eventdata, handles)
% hObject    handle to mn_image_mode (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --------------------------------------------------------------------
function mn_image_resize_Callback(hObject, eventdata, handles)
% hObject    handle to mn_image_resize (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[rows cols channels] = size(handles.img);
[answer width width_unit height height_unit interp] = ResizeDlg('width', rows, 'height', cols);

if isequal(answer, 'Yes')
    handles.undoimg = handles.img;
    if isequal(width_unit, 'pixels')
        rows = width;
    else
        rows = floor(rows * width/100);
    end
    if isequal(height_unit, 'pixels')
        cols = height;
    else
        cols = floor(cols * height/100);
    end
    handles.img = imresize(handles.img, [rows cols], interp);
%     imshow(handles.img, 'parent', handles.axesImg);
    updateScreen(handles);
else
    disp('Do nothing');
end
guidata(hObject, handles);

% --------------------------------------------------------------------
function mn_image_mode_rgb_Callback(hObject, eventdata, handles)
% hObject    handle to mn_image_mode_rgb (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --------------------------------------------------------------------
function uiresize_ClickedCallback(hObject, eventdata, handles)
% hObject    handle to uiresize (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
mn_image_resize_Callback(hObject, eventdata, handles);


% --------------------------------------------------------------------
function mn_filter_Callback(hObject, eventdata, handles)
% hObject    handle to mn_filter (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --------------------------------------------------------------------
function mn_filter_blur_Callback(hObject, eventdata, handles)
% hObject    handle to mn_filter_blur (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)



% --------------------------------------------------------------------
function mn_filter_blur_avrg_Callback(hObject, eventdata, handles)
% hObject    handle to mn_filter_blur_avrg (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

answer = inputdlg('Enter filter size:', 'Average filter size', 1, {'3'});
if ~isempty(answer)
    hsize = str2num(answer{1});
    handles.undoimg = handles.img;
    h = fspecial('average', hsize);
    handles.img = imfilter(handles.img, h, 'conv');
    imshow(handles.img, 'parent', handles.axesImg);
else
    disp('Do nothing');
end
guidata(hObject, handles);

% --------------------------------------------------------------------
function mn_filter_blur_gauss_Callback(hObject, eventdata, handles)
% hObject    handle to mn_filter_blur_gauss (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
answer = inputdlg({'Enter hsize:','Enter sigma:'} , 'Gaussian Params', 1, {'3', '0.5'});
if ~isempty(answer)
    hsize = str2num(answer{1});
    sigma = str2num(answer{2});
    handles.undoimg = handles.img;
    h = fspecial('gaussian', hsize, sigma);
    handles.img = imfilter(handles.img, h, 'conv');
    imshow(handles.img, 'parent', handles.axesImg);
else
    disp('Do nothing');
end
guidata(hObject, handles);

% --------------------------------------------------------------------
function mn_filter_blur_laplacian_Callback(hObject, eventdata, handles)
% hObject    handle to mn_filter_blur_laplacian (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
answer = inputdlg('Enter alpha:', 'Laplacian Params', 1, {'0.2'});
if ~isempty(answer)
    alpha = str2num(answer{1});
    handles.undoimg = handles.img;
    h = fspecial('laplacian', alpha);
    handles.img = imfilter(handles.img, h, 'conv');
    imshow(handles.img, 'parent', handles.axesImg);
else
    disp('Do nothing');
end
guidata(hObject, handles);

% --------------------------------------------------------------------
function mn_filter_blur_log_Callback(hObject, eventdata, handles)
% hObject    handle to mn_filter_blur_log (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
answer = inputdlg({'Enter hsize:','Enter sigma:'} , 'LoG Params', 1, {'5', '0.5'});
if ~isempty(answer)
    hsize = str2num(answer{1});
    sigma = str2num(answer{2});
    handles.undoimg = handles.img;
    h = fspecial('log', hsize, sigma);
    handles.img = imfilter(handles.img, h, 'conv');
    imshow(handles.img, 'parent', handles.axesImg);
else
    disp('Do nothing');
end
guidata(hObject, handles);

% --------------------------------------------------------------------
function mn_filter_blur_motion_Callback(hObject, eventdata, handles)
% hObject    handle to mn_filter_blur_motion (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
answer = inputdlg({'Enter len:','Enter theta:'} , 'Motion Params', 1, {'9', '0'});
if ~isempty(answer)
    len = str2num(answer{1});
    theta = str2num(answer{2});
    handles.undoimg = handles.img;
    h = fspecial('motion', len, theta);
    handles.img = imfilter(handles.img, h, 'conv');
    imshow(handles.img, 'parent', handles.axesImg);
else
    disp('Do nothing');
end
guidata(hObject, handles);

% --------------------------------------------------------------------
function mn_filter_sharpen_unsharp_Callback(hObject, eventdata, handles)
% hObject    handle to mn_filter_sharpen_unsharp (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
answer = inputdlg('Enter alpha:', 'Unsharp Params', 1, {'0.2'});
if ~isempty(answer)
    alpha = str2num(answer{1});
    handles.undoimg = handles.img;
    h = fspecial('unsharp', alpha);
    handles.img = imfilter(handles.img, h, 'conv');
    imshow(handles.img, 'parent', handles.axesImg);
else
    disp('Do nothing');
end
guidata(hObject, handles);


% --------------------------------------------------------------------
function mn_filter_sharpen_Callback(hObject, eventdata, handles)
% hObject    handle to mn_filter_sharpen (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --------------------------------------------------------------------
function mn_filter_sharpen_prewitt_Callback(hObject, eventdata, handles)
% hObject    handle to mn_filter_sharpen_prewitt (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
handles.undoimg = handles.img;
h = fspecial('prewitt');
handles.img = imfilter(handles.img, h, 'conv');
imshow(handles.img, 'parent', handles.axesImg);
guidata(hObject, handles);

% --------------------------------------------------------------------
function mn_filter_sharpen_sobel_Callback(hObject, eventdata, handles)
% hObject    handle to mn_filter_sharpen_sobel (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
handles.undoimg = handles.img;
h = fspecial('sobel');
handles.img = imfilter(handles.img, h, 'conv');
imshow(handles.img, 'parent', handles.axesImg);
guidata(hObject, handles);


% --------------------------------------------------------------------
function mn_filter_noise_Callback(hObject, eventdata, handles)
% hObject    handle to mn_filter_noise (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --------------------------------------------------------------------
function mn_filter_noise_gauss_Callback(hObject, eventdata, handles)
% hObject    handle to mn_filter_noise_gauss (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
answer = inputdlg({'Enter mean:','Enter variance:'} , 'Gaussian White Noise Params', 1, {'0', '0.01'});
if ~isempty(answer)
    m = str2num(answer{1});
    v = str2num(answer{2});
    handles.undoimg = handles.img;
    handles.img = imnoise(handles.img,'gaussian',m,v);
    imshow(handles.img, 'parent', handles.axesImg);
else
    disp('Do nothing');
end
guidata(hObject, handles);

% --------------------------------------------------------------------
function mn_filter_noise_poisson_Callback(hObject, eventdata, handles)
% hObject    handle to mn_filter_noise_poisson (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
handles.undoimg = handles.img;
handles.img = imnoise(handles.img,'poisson');
imshow(handles.img, 'parent', handles.axesImg);
guidata(hObject, handles);

% --------------------------------------------------------------------
function mn_filter_noise_saltpepper_Callback(hObject, eventdata, handles)
% hObject    handle to mn_filter_noise_saltpepper (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
answer = inputdlg('Enter noise density:', 'Salt & Pepper Params', 1, {'0.05'});
if ~isempty(answer)
    d = str2num(answer{1});
    handles.undoimg = handles.img;
%     handles.img = double(handles.img)/255;
    handles.img = imnoise(handles.img,'salt & pepper',d);     
    imshow(handles.img, 'parent', handles.axesImg);
else
    disp('Do nothing');
end
guidata(hObject, handles);

% --------------------------------------------------------------------
function mn_filter_noise_speckle_Callback(hObject, eventdata, handles)
% hObject    handle to mn_filter_noise_speckle (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
answer = inputdlg('Enter variance:', 'Speckle', 1, {'0.04'});
if ~isempty(answer)
    variance = str2num(answer{1});
    handles.undoimg = handles.img;
%     handles.img = double(handles.img)/255;
    handles.img = imnoise(handles.img,'speckle',variance);     
    imshow(handles.img, 'parent', handles.axesImg);
else
    disp('Do nothing');
end
guidata(hObject, handles);


% --------------------------------------------------------------------
function mn_image_adjust_histeq_Callback(hObject, eventdata, handles)
% hObject    handle to mn_image_adjust_histeq (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
handles.undoimg = handles.img;
if isRGBImg(handles.img)
    handles.img(:,:,1) = histeq(handles.img(:,:,1));
    handles.img(:,:,2) = histeq(handles.img(:,:,2));
    handles.img(:,:,3) = histeq(handles.img(:,:,3));
else
    handles.img = histeq(handles.img);
end
imshow(handles.img, 'parent', handles.axesImg);
guidata(hObject, handles);


% --------------------------------------------------------------------
function mn_image_mode_grayscale_Callback(hObject, eventdata, handles)
% hObject    handle to mn_image_mode_grayscale (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if isRGBImg(handles.img)
    confirm = questdlg('Sure? This is one-way transform');
    if isequal(confirm, 'Yes')
        handles.undoimg = handles.img;
        handles.img = rgb2gray(handles.img);
%         imshow(handles.img, 'parent', handles.axesImg);
%         set(hObject, 'Checked', 'on');
%         set(handles.mn_image_mode_rgb, 'Checked', 'off');
        updateScreen(handles);
    else
        disp('Do nothing');
    end
else
    disp('Was grayscale, do nothing');
end
guidata(hObject, handles);

% --------------------------------------------------------------------
function updateScreen(handles)
imshow(handles.img, 'parent', handles.axesImg);
[rows cols channels] = size(handles.img);
set(handles.mainfigure, 'Name', ['ImageLab-' '[' handles.filename ']-[' num2str(rows) 'x' num2str(cols) 'x' num2str(channels) ']']);
if isRGBImg(handles.img)
    set(handles.mn_image_mode_rgb, 'Checked', 'on');
    set(handles.mn_image_mode_grayscale, 'Checked', 'off');
else
    set(handles.mn_image_mode_rgb, 'Checked', 'off');
    set(handles.mn_image_mode_grayscale, 'Checked', 'on');
end


% --------------------------------------------------------------------
function mn_analysis_Callback(hObject, eventdata, handles)
% hObject    handle to mn_analysis (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --------------------------------------------------------------------
function mn_analysis_edgedetection_Callback(hObject, eventdata, handles)
% hObject    handle to mn_analysis_edgedetection (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if isRGBImg(handles.img)
    gray_img = rgb2gray(handles.img);
else
    gray_img = handles.img;
end
edge_img = edge(gray_img, 'canny');
figure('Name', 'Edge Detection', 'NumberTitle','off');
subplot(1, 2, 1); imshow(handles.img);title('Original Image');
subplot(1, 2, 2); imshow(edge_img);title('Edge Map');
guidata(hObject, handles);


% --------------------------------------------------------------------
function mn_analysis_fourier_Callback(hObject, eventdata, handles)
% hObject    handle to mn_analysis_fourier (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if isRGBImg(handles.img)
    gray_img = rgb2gray(handles.img);
else
    gray_img = handles.img;
end
Y = fft2(gray_img);
Y = fftshift(Y);
Y = log(abs(Y) + 1);
figure('Name', 'Fourier Domain', 'NumberTitle', 'off');
subplot(1, 2, 1); imshow(handles.img);title('Original Image');
subplot(1, 2, 2); imshow(Y, []);title('Magnitude Spectrum');
guidata(hObject, handles); 


% --------------------------------------------------------------------
function mn_image_adjust_histspec_Callback(hObject, eventdata, handles)
% hObject    handle to mn_image_adjust_histspec (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[filename pathname] = uigetfile({'*.jpg;*.tif;*.png;*.gif;*.bmp','All Image Files';...
    '*.*','All Files' },'Image Selector', '../imgs/');
if ~isequal(filename, 0)
    handles.undoimg = handles.img;
    dest_img = imread([pathname filename]);
    if isRGBImg(dest_img)
        gray_img = rgb2gray(dest_img);
    else
        gray_img = dest_img;
    end
    dest_hgram = imhist(gray_img);
    if isRGBImg(handles.img)                        
        handles.img(:,:,1) = histeq(handles.img(:,:,1), dest_hgram);
        handles.img(:,:,2) = histeq(handles.img(:,:,2), dest_hgram);
        handles.img(:,:,3) = histeq(handles.img(:,:,3), dest_hgram);
    else
        handles.img = histeq(handles.img, dest_hgram);
    end
    imshow(handles.img, 'parent', handles.axesImg);
else
    disp('Do nothing');
end
guidata(hObject, handles);